package com.king.base;

import java.util.List;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.king.print.Printer;

public class PredicateTest {

	public static final Predicate<String> NOT_NULL_PREDICATE = new Predicate<String>() {
		@Override
		public boolean apply(String s) {
			return s != null;
		}
	};

	public static final Predicate<String> EMPTY_PREDICATE = new Predicate<String>() {
		@Override
		public boolean apply(String s) {
			return "".equals(s);
		}
	};

	public static final Predicate<String> NOT_EMPTY_PREDICATE = Predicates.not(EMPTY_PREDICATE);

	public static final Predicate<String> SPACE_PREDICATE = new Predicate<String>() {
		@Override
		public boolean apply(String s) {
			return " ".equals(s);
		}
	};

	public static final Predicate<String> NOT_SPACE_PREDICATE = Predicates.not(SPACE_PREDICATE);

	public static final Predicate<String> BLANK_PREDICATE = new Predicate<String>() {
		@Override
		public boolean apply(String s) {
			return s.length() > 0 && "".equals(s.trim());
		}
	};

	public static final Predicate<String> NOT_BLANK_PREDICATE = Predicates.not(BLANK_PREDICATE);

	public static final Function<String, String> UPPER_FUNCTION = new Function<String, String>() {
		@Override
		public String apply(String s) {
			return s.toUpperCase();
		}
	};


	public static void main(String[] args) {

		List<String> list = Lists.newArrayList(null, "ac", "fefsdf", "", " ", "  ");

		// FIRST remove null element
		Iterable<String> result1 = Iterables.filter(list, NOT_NULL_PREDICATE);
		Printer.print(result1);

		// THEN remove empty element
		result1 = Iterables.filter(result1, NOT_EMPTY_PREDICATE);
		Printer.print(result1);

		// THEN remove space element
		result1 = Iterables.filter(result1, NOT_SPACE_PREDICATE);
		Printer.print(result1);

		// AT LAST remove blank element
		result1 = Iterables.filter(result1, NOT_BLANK_PREDICATE);
		Printer.print(result1);


		// OK, let's do all at one time
		Predicate<String> all = Predicates.and(NOT_NULL_PREDICATE, NOT_EMPTY_PREDICATE, NOT_SPACE_PREDICATE, NOT_BLANK_PREDICATE);
		Printer.print(Iterables.filter(list, all));

		// notice: just a demo, not worth doing like this!!!
		Iterable<String> result = Iterables.transform(Iterables.filter(list, NOT_NULL_PREDICATE), UPPER_FUNCTION);
		Printer.print(result);
	}
}
